package api

import (
	"context"

	"dev.ixmxm.top/rms/codes"
	"dev.ixmxm.top/rms/utils/log"
)

// @Summary 上传地堆库数据
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body uploadGroundLocationParams true "json地堆库数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 地堆库
// @Router /location/upload_ground_location [post]
func (hg *HttpGateway) UploadGroundLocation(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*uploadGroundLocationParams)
	//获得一键急停
	cfg := *hg.dispatcher.SystemConfig
	if !cfg.IsSoftStop {
		subtasks, err := hg.dispatcher.TM().GetAllSubtaskFromDB(ctx)
		if err != nil {
			return nil, err.WithDetails("获取所有子任务失败")
		}
		for _, subtask := range subtasks {
			if subtask.MapName == p.MapName {
				return nil, codes.ErrTaskExists.WithDetails("存在运行的任务，不可修改地图")
			}
		}
	}
	if errUpdate := hg.dispatcher.WMM().SaveGroundLocationToDB(ctx, &p.GroundLocation); errUpdate != nil {
		return nil, errUpdate
	}

	return nil, nil
}

// @Summary 修改单个地堆库组
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body  model.OneGroundLocation true "更新单个地堆库数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 地堆库
// @Router /location/update_ground_location [post]
func (hg *HttpGateway) UpdateGroundLocation(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*updateOnGroundLocationParams)
	//获得一键急停
	cfg := *hg.dispatcher.SystemConfig
	if !cfg.IsSoftStop {
		subtasks, err := hg.dispatcher.TM().GetAllSubtaskFromDB(ctx)
		if err != nil {
			return nil, err.WithDetails("获取所有子任务失败")
		}
		for _, subtask := range subtasks {
			if subtask.MapName == p.MapName {
				return nil, codes.ErrTaskExists.WithDetails("存在运行的任务，不可修改地图")
			}
		}
	}
	if errUpdate := hg.dispatcher.WMM().UpdateGroudLocationToDB(ctx, &p.OneGroundLocation); errUpdate != nil {
		return nil, errUpdate
	}

	return nil, nil
}

// @Summary 删除指定地图下所有地堆库组
// @Description
// @Accept application/json
// @Produce application/json
// @Param map_name query string true "地图名称"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 地堆库
// @Router /location/delete_ground_location [delete]
func (hg *HttpGateway) DeleteGroundLocationByMapName(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*deleteGroundLocationParams)
	//获得一键急停
	cfg := *hg.dispatcher.SystemConfig
	if !cfg.IsSoftStop {
		subtasks, err := hg.dispatcher.TM().GetAllSubtaskFromDB(ctx)
		if err != nil {
			return nil, err.WithDetails("获取所有子任务失败")
		}
		for _, subtask := range subtasks {
			if subtask.MapName == p.MapName {
				return nil, codes.ErrTaskExists.WithDetails("存在运行的任务，不可修改地图")
			}
		}
	}
	if errUpdate := hg.dispatcher.WMM().DeleteAllGroudLocationToDB(ctx, p.MapName); errUpdate != nil {
		return nil, errUpdate
	}

	return nil, nil
}

// @Summary 删除单个地图下的单个库位
// @Description
// @Accept application/json
// @Produce application/json
// @Param map_name query string true "地图名称"
// @Param group query string true "地堆库组号"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 地堆库
// @Router /location/delete_one_location [delete]
func (hg *HttpGateway) DeleteOneLocationByMapName(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*deleteOneLocationParams)
	//获得一键急停
	cfg := *hg.dispatcher.SystemConfig
	if !cfg.IsSoftStop {
		subtasks, err := hg.dispatcher.TM().GetAllSubtaskFromDB(ctx)
		if err != nil {
			return nil, err.WithDetails("获取所有子任务失败")
		}
		for _, subtask := range subtasks {
			if subtask.MapName == p.MapName {
				return nil, codes.ErrTaskExists.WithDetails("存在运行的任务，不可修改地图")
			}
		}
	}
	if errUpdate := hg.dispatcher.WMM().DeleteOneGroudLocationToDB(ctx, p.MapName, p.Group); errUpdate != nil {
		return nil, errUpdate
	}

	return nil, nil
}

// @Summary 获得地堆库数据
// @Description
// @Accept application/json
// @Produce application/json
// @Success 0 {object} ResponseExampleList "返回数据格式"
// @Success 1 {object} []model.GroundLocation "data数据：json 地堆库数组"
// @Tags 地堆库
// @Router /location/get_all_ground_locations [get]
func (hg *HttpGateway) GetGroundLocations(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	location, err := hg.dispatcher.WMM().GetAllGroundLocations(ctx)
	if err != nil {
		log.ErrorfWithContext(ctx, "get location error:%s", err.UnWrapError())
		return nil, err
	}

	return location, nil
}

// @Summary 获得地堆库数据
// @Description
// @Accept application/json
// @Produce application/json
// @Param map_name query string true "地图名称"
// @Success 0 {object} model.GroundLocation "data数据：json 地堆库数据"
// @Success 1 {object} ResponseExampleList "返回数据格式"
// @Tags 地堆库
// @Router /location/get_ground_locations [get]
func (hg *HttpGateway) GetGroundLocationsByMapName(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*getGroundLocationsByMapNameParams)
	location, err := hg.dispatcher.WMM().GetAllGroundLocationsByMapName(ctx, p.MapName)
	if err != nil {
		log.ErrorfWithContext(ctx, "get location error:%s", err.UnWrapError())
		return nil, err
	}

	return location, nil
}
